﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manul.Genetics.Character;
using Manul.Interface.TreeNodes;

namespace Manul.GenomCreationTest
{
    public partial class FormEpistasis : Form
    {
        public FormEpistasis()
        {
            InitializeComponent();
        }

        private void FormEpistasis_Load(object sender, EventArgs e)
        {
            if (CurrentCharacters != null)
            {
                treeViewEpistasis.LoadCharacters(CurrentCharacters, false);
                List<TreeNode> NodesToCheck = new List<TreeNode>();
                foreach (TreeNode node in treeViewEpistasis.Nodes)
                    NodesToCheck.Add(node);
                int i = 0;
                Character characterToCheck = null;
                while (i < NodesToCheck.Count)
                {
                    if (NodesToCheck[i].Nodes.Count > 0)
                        foreach (TreeNode node in NodesToCheck[i].Nodes)
                            NodesToCheck.Add(node);
                    if (NodesToCheck[i] is ObjectTreeNode<Character>)
                    {
                        characterToCheck = (NodesToCheck[i] as ObjectTreeNode<Character>).Obj;
                        if (characterToCheck == CurrentCharacter || CurrentEpistasis.Contains(characterToCheck))
                            treeViewEpistasis.Nodes.Remove(NodesToCheck[i]);
                    }
                    i++;
                }
                NodesToCheck.Clear();
                foreach (TreeNode node in treeViewEpistasis.Nodes)
                {
                    if (node is ObjectTreeNode<CharacterDirectory>)
                        (node as ObjectTreeNode<CharacterDirectory>).UpdateRealCharacterItemsCount<ObjectTreeNode<CharacterDirectory>,
                                                                                                   ObjectTreeNode<Character>>();
                    NodesToCheck.Add(node);
                }
                i = 0;
                while (i < NodesToCheck.Count)
                {
                    if (NodesToCheck[i] is ObjectTreeNode<CharacterDirectory>)
                    {
                        if((NodesToCheck[i] as ObjectTreeNode<CharacterDirectory>).RealCharacterCount > 0)
                        {
                            foreach (TreeNode node in NodesToCheck[i].Nodes)
                                NodesToCheck.Add(node);
                        }
                        else
                        {
                            treeViewEpistasis.Nodes.Remove(NodesToCheck[i]);
                            NodesToCheck.RemoveAt(i);
                        }
                    }
                    i++;
                }
            }
        }
        private Character currentCharacter;
        public Character CurrentCharacter
        {
            get
            {
                return currentCharacter;
            }
            set
            {
                currentCharacter = value;
                CurrentEpistasis.Clear();
                if (value != null)
                    CurrentEpistasis.AddRange(currentCharacter.Epistasis);
            }
        }
        public List<Character> CurrentEpistasis = new List<Character>();

        public CharacterDirectory CurrentCharacters = null;

        private void buttonOK_Click(object sender, EventArgs e)
        {
            List<CharacterDirectory> ToEpistasis = new List<CharacterDirectory>();
            foreach (TreeNode node in treeViewEpistasis.SelectedNodes)
            {
                if (node is ObjectTreeNode<CharacterDirectory>)
                    ToEpistasis.Add((node as ObjectTreeNode<CharacterDirectory>).Obj);
                else if (node is ObjectTreeNode<Character> && (node as ObjectTreeNode<Character>).Obj != CurrentCharacter)
                    CurrentEpistasis.Add((node as ObjectTreeNode<Character>).Obj);
            }
            int i = 0;
            CharacterDirectory characterDirectory = null;
            while (i < ToEpistasis.Count)
            {
                characterDirectory = ToEpistasis[i] as CharacterDirectory;
                foreach(CharacterItem charItem in characterDirectory.Characters)
                {
                    if (charItem is CharacterDirectory)
                        ToEpistasis.Add((CharacterDirectory)charItem);
                    else if (charItem is Character && charItem != CurrentCharacters)
                        CurrentEpistasis.Add((Character)charItem);
                }
                i++;
            }
        }
    }
}
